package com.evaluation.kexingcp.psycho.evaluation.sys.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.evaluation.kexingcp.common.core.util.R;
import com.evaluation.kexingcp.common.excel.annotation.ResponseExcel;
import com.evaluation.kexingcp.common.log.annotation.SysLog;
import com.evaluation.kexingcp.common.security.annotation.Inner;
import com.evaluation.kexingcp.psycho.evaluation.sys.entity.EvaluationQuestionsEntity;
import com.evaluation.kexingcp.psycho.evaluation.sys.service.EvaluationQuestionsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 测评量表试题
 *
 * @author 九炜
 * @date 2024-07-01 09:47:44
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/evaluationQuestions" )
@Tag(description = "evaluationQuestions" , name = "测评量表试题管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class EvaluationQuestionsController {

    private final  EvaluationQuestionsService evaluationQuestionsService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param evaluationQuestions 测评量表试题
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page")
    public R getEvaluationQuestionsPage(@ParameterObject Page page, @ParameterObject EvaluationQuestionsEntity evaluationQuestions) {
        if (StrUtil.isEmpty(evaluationQuestions.getScaleId())) {
            page.setRecords(null);
            return R.ok(page);
        }

        LambdaQueryWrapper<EvaluationQuestionsEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EvaluationQuestionsEntity::getScaleId,evaluationQuestions.getScaleId());
        return R.ok(evaluationQuestionsService.page(page, wrapper));
    }

    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/list")
    @Inner(value = false)
    public R list(@ParameterObject EvaluationQuestionsEntity evaluationQuestions) {
        LambdaQueryWrapper<EvaluationQuestionsEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EvaluationQuestionsEntity::getScaleId,evaluationQuestions.getScaleId());
        return R.ok(evaluationQuestionsService.list(wrapper));
    }


    /**
     * 通过id查询测评量表试题
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('evaluation_evaluationScale_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(evaluationQuestionsService.getById(id));
    }

    /**
     * 新增测评量表试题
     * @param evaluationQuestions 测评量表试题
     * @return R
     */
    @Operation(summary = "新增测评量表试题" , description = "新增测评量表试题" )
    @SysLog("新增测评量表试题" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('evaluation_evaluationScale_add')" )
    public R save(@RequestBody EvaluationQuestionsEntity evaluationQuestions) {
        return R.ok(evaluationQuestionsService.save(evaluationQuestions));
    }

    /**
     * 修改测评量表试题
     * @param evaluationQuestions 测评量表试题
     * @return R
     */
    @Operation(summary = "修改测评量表试题" , description = "修改测评量表试题" )
    @SysLog("修改测评量表试题" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('evaluation_evaluationScale_edit')" )
    public R updateById(@RequestBody EvaluationQuestionsEntity evaluationQuestions) {
        return R.ok(evaluationQuestionsService.updateById(evaluationQuestions));
    }

    /**
     * 通过id删除测评量表试题
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除测评量表试题" , description = "通过id删除测评量表试题" )
    @SysLog("通过id删除测评量表试题" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('evaluation_evaluationScale_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(evaluationQuestionsService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param evaluationQuestions 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('evaluation_evaluationScale_export')" )
    public List<EvaluationQuestionsEntity> export(EvaluationQuestionsEntity evaluationQuestions,Long[] ids) {
        return evaluationQuestionsService.list(Wrappers.lambdaQuery(evaluationQuestions).in(ArrayUtil.isNotEmpty(ids), EvaluationQuestionsEntity::getId, ids));
    }
}